package com.yunchao.feature.business.domain.model

import com.yunchao.feature.business.data.datasource.database.model.BusinessPackEntityModel
import com.yunchao.feature.business.domain.enum.BusinessType
import com.yunchao.library.lock.LockEPCInfo
import com.yunchao.library.lock.getVoucherTypeId
import com.yunchao.library.lock.getVoucherTypeName
import com.yunchao.library.lock.getVoucherTypeValue
import timber.log.Timber
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

internal data class BusinessPackModel(
    val voucherTypeName: String,//  String 15 N  券别名称
    val value: String,//  String 10 N  面额
    val paperTypeID: String,//  String 1  N  券别说明ID
    val paperTypeName: String,//    String 15 N  券别说明名称
    val sackMoney: String,//String  18,2   N  封装金额
    val sackNo: String,//String 50 N  锁条号
    val voucherTypeId: String,//    String 6  N  券别ID
    val bundles: String,//  String 2  N  捆数
    val tie: String,//  String 3  N  把数
    val oprDT: String,//    String 14 N  封装时间
    val totalMoney: String,//   String 18,2   N  总金额
    val mustOutFlag: String,//String   1  N  是否必出箱
    val handleResult: Boolean = false,
    val lockEPCInfo: LockEPCInfo? = null,

    val currency: String,
    val condomId: String,
    val condomName: String,
    val editionId: String,
    val editionName: String,
    val number: String,
    val flag: String,
    val scanResult: String
)

internal fun BusinessPackModel.toEntityModel(): BusinessPackEntityModel = BusinessPackEntityModel(
    voucherTypeName = voucherTypeName,
    value = value,
    paperTypeID = paperTypeID,
    paperTypeName = paperTypeName,
    sackMoney = sackMoney,
    sackNo = sackNo,
    voucherTypeId = voucherTypeId,
    bundles = bundles,
    tie = tie,
    oprDT = oprDT,
    totalMoney = totalMoney,
    mustOutFlag = mustOutFlag,
    handleResult = handleResult,
    lockEPC = lockEPCInfo?.epc,
    currency = currency,
    condomId = condomId,
    condomName = condomName,
    editionId = editionId,
    editionName = editionName,
    number = number,
    flag = flag,
    scanResult = scanResult
)

internal fun createPackInfoByLock(lockEPCInfo: LockEPCInfo): BusinessPackModel = BusinessPackModel(
    voucherTypeName = lockEPCInfo.getVoucherTypeName(),
    voucherTypeId = lockEPCInfo.getVoucherTypeId(),
    sackNo = lockEPCInfo.id,
    value = lockEPCInfo.getVoucherTypeValue(),
    lockEPCInfo = lockEPCInfo,
    handleResult = true,
    oprDT = currentTimeToYyyyMMddHHmmss(),

    paperTypeID = "",
    paperTypeName = "",
    sackMoney = "",
    bundles = "",
    tie = "",
    totalMoney = "",
    mustOutFlag = "",
    condomId = "",
    condomName = "",
    editionId = "",
    editionName = "",
    number = "",
    flag = "",
    scanResult = "",
    currency = "",
)


internal fun BusinessPackModel.getPackInfoByLock(
    tranTypeId: String,
    lockEPCInfo: LockEPCInfo,
    busiInfo: BusinessPackModel? = null,
    handleResult: Boolean = true
): BusinessPackModel {
    val (bundles, tie, money) = if (BusinessType.BusinessTypeMIMISIF003.type == tranTypeId) {
        val pageNumber = lockEPCInfo.pageNumber
        when {
            pageNumber < TIE -> Triple(
                0, 0, BigDecimal(pageNumber).multiply(BigDecimal(lockEPCInfo.getVoucherTypeValue()))
                    .toString()
            )

            pageNumber >= BUNDLES -> {
                val remainder = pageNumber % BUNDLES
                val tie = if (remainder < TIE) 0 else remainder / TIE
                Triple(
                    pageNumber / BUNDLES,
                    tie,
                    BigDecimal(pageNumber).multiply(BigDecimal(lockEPCInfo.getVoucherTypeValue()))
                        .toString()
                )
            }

            else -> Triple(
                0,
                pageNumber / TIE,
                BigDecimal(pageNumber).multiply(BigDecimal(lockEPCInfo.getVoucherTypeValue()))
                    .toString()
            )
        }
    } else {
        val bundles = this.bundles.toIntOrNull() ?: 0
        val tie = this.tie.toIntOrNull() ?: 0
        Triple(bundles, tie, this.sackMoney)
    }

    /**
    val condomId: String = "",
    val condomName: String = "",
    val editionId: String = "",
    val editionName: String = "",
    val number: String = "",
    val flag: String = "",
    val scanResult: String = "",
    val giveOperator: String = "",
    val giveDataTime: String = "",
    val currency: String = "",
     */
    return BusinessPackModel(
        paperTypeID = busiInfo?.paperTypeID ?: this.paperTypeID,
        paperTypeName = busiInfo?.paperTypeName ?: this.paperTypeName,
        voucherTypeId = busiInfo?.voucherTypeId ?: this.voucherTypeId,
        voucherTypeName = busiInfo?.voucherTypeName ?: this.voucherTypeName,
        value = this.value.ifBlank { lockEPCInfo.getVoucherTypeValue() },
        sackMoney = money,
        sackNo = this.lockEPCInfo?.id ?: this.sackNo,
        bundles = bundles.toString(),
        tie = tie.toString(),
        oprDT = currentTimeToYyyyMMddHHmmss(),
        totalMoney = this.totalMoney,
        mustOutFlag = this.mustOutFlag,
        lockEPCInfo = lockEPCInfo,
        handleResult = handleResult,
        condomId = busiInfo?.condomId ?: this.condomId,
        condomName = busiInfo?.condomName ?: this.condomName,
        editionId = busiInfo?.editionId ?: this.editionId,
        editionName = busiInfo?.editionName ?: this.editionName,
        number = busiInfo?.number ?: this.number,
        flag = busiInfo?.flag ?: this.flag,
        scanResult = busiInfo?.scanResult ?: this.scanResult,
        currency = busiInfo?.currency ?: this.currency
    )
}

private const val BUNDLES = 1000 // 每包的数量
private const val TIE = 100 // 每捆的数量


fun currentTimeToYyyyMMddHHmmss(): String {
    // 获取当前时间
    val calendar = Calendar.getInstance()
    // 定义日期时间格式
    val formatter = SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault())
    // 格式化当前时间
    val formattedDateTime = formatter.format(calendar.time)

    Timber.d("当前时间: $formattedDateTime")
    return formattedDateTime
}